package com.item.yxlottery.yxlottery.fragment;

/*
 *------------------------------
 * @创建者      Administrator
 * @创建时间    2016/11/16 15:41
 * @文件描述    账户fragment
 *------------------------------
 */

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.item.yxlottery.yxlottery.R;
import com.item.yxlottery.yxlottery.activity.LoginActivity;
import com.item.yxlottery.yxlottery.activity.MyOrdersActivity;
import com.item.yxlottery.yxlottery.activity.RechargeActivity;
import com.item.yxlottery.yxlottery.activity.UserAccountActivity;
import com.item.yxlottery.yxlottery.activity.WithdrawActivity;
import com.item.yxlottery.yxlottery.base.BaseActivity;
import com.item.yxlottery.yxlottery.base.BaseFragment;
import com.item.yxlottery.yxlottery.utils.UrlUtil;
import com.orhanobut.hawk.Hawk;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;


public class AccountFragment extends BaseFragment implements View.OnClickListener{

    @Bind(R.id.fg_account_iv_minelogo)
    ImageView mIvMinelogo;//头像
    @Bind(R.id.fg_account_tv_username)
    TextView mTvUsername;//用户名
    @Bind(R.id.fg_account_tv_balance)
    TextView mTvBalance;//余额
    @Bind(R.id.fg_account_ll_recharge)
    AutoLinearLayout mLlRecharge;//充值
    @Bind(R.id.fg_account_ll_withdraw)
    AutoLinearLayout mLlWithdraw;//提现
    @Bind(R.id.fg_account_ll_record)
    AutoLinearLayout mLlRecord;//投注记录
    @Bind(R.id.fg_account_ll_bill)
    AutoLinearLayout mLlBill;//账单明细
    @Bind(R.id.fg_account_ll_chasenumber)
    AutoLinearLayout mLlChasenumber;//追号管理
    @Bind(R.id.fg_account_ll_coupon)
    AutoLinearLayout mLlCoupon;//优惠券
    @Bind(R.id.fg_account_ll_score_bg)
    AutoLinearLayout mLlScore;//我的彩分
    @Bind(R.id.fg_account_ll_login)
    AutoLinearLayout mLlLogin;//已登陆
    @Bind(R.id.fg_account_tv_logintips)
    TextView mTvLogintips;//未登陆
    @Bind(R.id.fg_account_ll_logininfo)
    AutoLinearLayout mLlLogininfo;//登陆信息框

    public Context mContext;
    private String mMemberTruename;
    private String mMemberName;
    private String mMemberAvatar;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            mContext = getActivity();
        }
    }

    @Override
    protected void initEvent() {
        mLlRecharge.setOnClickListener(this);
        mLlWithdraw.setOnClickListener(this);
        mLlRecord.setOnClickListener(this);
        mLlBill.setOnClickListener(this);
        mLlChasenumber.setOnClickListener(this);
        mLlCoupon.setOnClickListener(this);
        mLlScore.setOnClickListener(this);
        mLlLogininfo.setOnClickListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_account, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Object url = R.drawable.minebg;
        if (Hawk.get("appToken","").equals("")){
            mLlLogin.setVisibility(View.GONE);
            mTvLogintips.setVisibility(View.VISIBLE);
        }else {
            //登陆状态
            // 用户真实姓名
            mMemberTruename = Hawk.get("memberTruename","");
            // 帐号名，手机号
            mMemberName = Hawk.get("memberName","");
            // 用户头像地址
            mMemberAvatar = Hawk.get("memberAvatar","");

            mLlLogin.setVisibility(View.VISIBLE);
            mTvLogintips.setVisibility(View.GONE);

            if (!mMemberAvatar.equals("")){
                url = UrlUtil.getHomeUrl()+ mMemberAvatar;
                //url = "http://test.yiqiquan.net/upload/img/adv/1476355138828.jpg";
            }

            mTvUsername.setText(mMemberName);
            mTvBalance.setText("null");
        }

        Glide.with(this).load(url)
                .asBitmap().centerCrop().into(new BitmapImageViewTarget(mIvMinelogo) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                mIvMinelogo.setImageDrawable(circularBitmapDrawable);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fg_account_ll_recharge://充值

                startActivity(new Intent(mContext, RechargeActivity.class));

                break;
            case R.id.fg_account_ll_withdraw://提现
                startActivity(new Intent(mContext, WithdrawActivity.class));
                break;
            case R.id.fg_account_ll_record://投注记录
                startActivity(new Intent(mContext, MyOrdersActivity.class));
                break;
            case R.id.fg_account_ll_bill://账单明细

                break;
            case R.id.fg_account_ll_chasenumber://账单明细

                break;
            case R.id.fg_account_ll_coupon://优惠券

                break;
            case R.id.fg_account_ll_score_bg://我的彩分

                break;
            case R.id.fg_account_ll_logininfo://点击登陆信息框
                if(Hawk.get("appToken","") == null || Hawk.get("appToken","").equals("")){
                    startActivity(new Intent(mContext, LoginActivity.class));
                }else {
                    Intent intent = new Intent(mContext, UserAccountActivity.class);
                    intent.putExtra("mMemberTruename",mMemberTruename);
                    intent.putExtra("mMemberName",mMemberName);
                    startActivity(intent);
                }

                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
